﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;

namespace LogRT
{

    /// <summary>
    /// 委托别人帮忙来处理接收到的数据
    /// </summary>
    /// <param name="logData">数据数组</param>
    /// <param name="dataLen">数据长度</param>
    public delegate bool ProcessDelegate(Byte[] logData, int dataLen);


    class UdpServerBasedLogGet
    {
        //接收数据线程相关
        private Thread m_udpRecvThread = null;
        private bool m_goonRecv = true;
        private object m_udpRecvMutex = new object();

        //端口
        private int m_udpRecvPort = 0;

        //数据处理委托
        public ProcessDelegate PushData;

        public bool GoonRecv()
        {
            lock (m_udpRecvMutex)
            {
                return m_goonRecv;
            }
        }

        public bool Start(int port)
        {
            Debug.Assert(null == m_udpRecvThread);
            Debug.Assert(port >= 1024 && port < 65536);
            m_udpRecvPort = port;

            lock (m_udpRecvMutex)
            {
                m_goonRecv = true;
            }

            m_udpRecvThread = new Thread(UdpServerBasedLogGet.UdpRecvThreadProc);
            m_udpRecvThread.Start((object)this);

            return true;
        }

        public bool Stop()
        {

            Debug.Assert(null != m_udpRecvThread);

            lock (m_udpRecvMutex)
            {
                m_goonRecv = false;
            }

            m_udpRecvThread.Join();

            return true;
        }

        static private void UdpRecvThreadProc(object data)
        {
            UdpServerBasedLogGet udpRecver = (UdpServerBasedLogGet)data;

            uint index = 0;
            int level = 0;
            while (udpRecver.GoonRecv())
            {

                //TODO: udp recv

                Thread.Sleep(50);

                string str;
                string dstr = "dfafdsafsfd.\n";
                if (level == 0)
                {
                    str = "[Debug]_" + dstr;
                    ++level;
                }
                else if (level == 1)
                {
                    str = "[ Info]_" + dstr;
                    ++level;
                }
                else if (level == 2)
                {
                    str = "[ Warn]_" + dstr;
                    ++level;
                }
                else if (level == 3)
                {
                    str = "[Error]_" + dstr;
                    ++level;
                }
                else
                {
                    str = "[xxxxx]_" + dstr;
                    level = 0;
                }

                Byte[] b = new Byte[str.Length + 23];
                System.BitConverter.GetBytes(index++).CopyTo(b, 19);
                System.Text.Encoding.Default.GetBytes(str).CopyTo(b, 23);
                b[0] = (Byte)'L';
                b[1] = (Byte)'R';
                b[2] = (Byte)'T';
                uint len = (uint)str.Length + 16;
                BitConverter.GetBytes(len).CopyTo(b, 3);
                udpRecver.PushData(b, b.Length);

            }
        }
    }
}
